<script type="text/ecmascript-6">
    import { Navbar, Submit } from 'vmc';
    import Verify from '../../components/verify-code.vue';
    import { Cache, cache_keys, env, is } from '../../drivers/utils';
    import { quickLogin, openidLogin, getOpenid } from '../../drivers/oauth';

    export default {
        components: {
            Navbar,
            Submit,
            Verify
        },
        data() {
            return {
                mobile: '',
                code: ''
            }
        },
        methods: {
            login() {
                var message = null;

                if (!is.has(this.mobile)) {
                    message = '请输入您的手机号';
                } else if (!is.mobile(this.mobile)) {
                    message = '手机号格式不正确，请重新输入';
                } else if (!is.has(this.code)) {
                    message = '请输入验证码';
                }

                if (message) {
                    this.$refs.button.done();
                    this.$Alert(message);
                } else {
                    quickLogin.call(this);
                }
            }
        },
        route: {
            activate(t) {
                if (env.isWechat) {
                    var type = this.$route.query.type;
                    var openid = this.$route.query.openid || this.$route.query['?openid'];
                    if (openid && type === 'oauth') {
                        // 查询该openid是否可以登录
                        openidLogin.call(this, {
                            onSuccess: null,
                            onError: function () {
                                t.next();
                            }
                        });
                    } else {
                        // 直接去取openid
                        getOpenid.call(this);
                    }
                } else {
                    t.next();
                }
            }
        }

    }
</script>